package com.gph.saviorframework.util;

import com.gph.saviorframework.common.model.SecurityEntity;
import com.gph.saviorframework.common.model.User;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Created by iscas-qd on 2016/3/7.
 */
@Component
public class SecurityUtils {

    public User getUser() {
        Object obj = org.apache.shiro.SecurityUtils.getSubject().getPrincipal();
        if (obj instanceof User) {
            return (User) obj;
        }
        else {
            return new User(obj.toString());
        }
    }

    public void setCreateEntity(SecurityEntity entity) {
        Date date = new Date();
        entity.setCreated(date);
        entity.setCreator(getUser());
        entity.setModified(date);
        entity.setModifier(getUser());
    }

    public void setCreateEntity(SecurityEntity entity,User user){
        Date date = new Date();
        entity.setCreated(date);
        entity.setCreator(user);
        entity.setModified(date);
        entity.setModifier(user);
    }

    public void setModifyEntity(SecurityEntity entity) {
        entity.setModified(new Date());
        entity.setModifier(getUser());
    }

    public void setModifyEntity(SecurityEntity entity,User user) {
        entity.setModified(new Date());
        entity.setModifier(user);
    }

    public String getUserFullName() {
        String str = getUser().getUsername() + "|" + getUser().getName();
        return str;
    }
}
